package com.yuke.cloud.service.smc.job;

import com.yuke.cloud.service.smc.entity.DmsStore;
import com.yuke.cloud.service.smc.service.DmsStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 定时清空跟近人，当该业务员跟进店铺一个月无进展时，要更换跟进人。
 * @author: 金天
 */
@Component
@Slf4j
@EnableScheduling
public class ClearDevUserIdQuartz {

    @Resource
    private DmsStoreService dmsStoreService;
    /**
     * 每天业务员跟进人清空
     * 每天一点
     */
    @Scheduled(cron =  "0 0 1 * * ?")
    public void scheduledMethod() {

        Date date = new Date();

        long monthTime = 31;

        //查询所有店铺
        Example example = new Example(DmsStore.class);

        Example.Criteria criteria = example.createCriteria();

        //如果跟进人不为空不为0,
        criteria.andIsNotNull("devUserId");

        criteria.andNotEqualTo("devUserId", 0);
        //时间不为null
        criteria.andIsNotNull("followTime");

        //服务人为空或者为零
        Example.Criteria criteria02 = example.createCriteria();

        criteria02.andIsNull("shopId").orEqualTo("shopId", 0);

        example.and(criteria02);

        List<DmsStore> allDmsStore = dmsStoreService.selectByExample(example);

        if (allDmsStore.size() > 0 && allDmsStore != null) {

            for (DmsStore dmsStore : allDmsStore) {
                //跟进时间大于31天
                long day = (dmsStore.getFollowTime().getTime() - date.getTime()) / (24 * 60 * 60 * 1000);

                if (day > monthTime) {
                    dmsStoreService.quitDevStore(dmsStore.getStoreId());
                }
            }
        }
    }
}
